<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Debounce</title>
  <style>
  html, body {
    width: 100%;
    height: 100%;
    padding: 0;
    margin: 0;
  }
  #panel {
    width: 100%;
    height: 0;
    padding-bottom: 100%;
  }
  </style>
</head>
<body>
  <div id="panel">
    <canvas></canvas>
  </div>
  <script>
    function debounce(fn, ms = 100) {
      let debounceTimer = null;
      return function(...args) {
        if(debounceTimer) clearTimeout(debounceTimer);

        debounceTimer = setTimeout(() => {
          fn.apply(this, args);
        }, ms);
      }
    }

    const panel = document.getElementById('panel');
    const canvas = document.querySelector('canvas');
    function resize() {
      canvas.width = panel.clientWidth;
      canvas.height = panel.clientHeight;
    }
    function draw() {
      const context = canvas.getContext('2d');
      const radius = canvas.width / 2;
      context.save();
      context.translate(radius, radius);
      for(let i = radius; i >= 0; i -= 5) {
        context.fillStyle = `hsl(${i % 360}, 50%, 50%)`;
        context.beginPath();
        context.arc(0, 0, i, i, 0, Math.PI * 2);
        context.fill();
      }
      context.restore();
    }

    resize();
    draw();

    window.addEventListener('resize', debounce(() => {
      resize();
      draw();
    }, 500));
  </script>
</body>
</html>